import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**服务器信息属性枚举*/
export const enum E_PlayerServerSnapshot {
	/**创建时间*/
	CREATE_TIME = 0,
	/**服务器id*/
	SERVER_ID = 1,
	/**服务器名*/
	SERVER_NAME = 2,
	/**枚举数量*/
	EnumCount = 3
}


/**服务器信息*/
export class PlayerServerSnapshot {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_PLAYER_SERVER_SNAPSHOT_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_PLAYER_SERVER_SNAPSHOT_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: PlayerServerSnapshot[] = [];

	/**创建入口*/
	public static create(json?: string): PlayerServerSnapshot {
		const c = this.POOL.pop() || new PlayerServerSnapshot();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**创建时间 (long)*/
	protected _createTime: number;
	/**服务器id (uint16)*/
	protected _serverId: number;
	/**服务器名*/
	protected _serverName: string;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (PlayerServerSnapshot.POOL.indexOf(this) === -1) {
			PlayerServerSnapshot.POOL.push(this);
		}
	}

	/**创建时间*/
	public get createTime() {
		return this._createTime;
	}


	/**创建时间*/
	public set createTime(v: number) {
		this._createTime = v;
	}


	/**服务器id*/
	public get serverId() {
		return this._serverId;
	}


	/**服务器id*/
	public set serverId(v: number) {
		this._serverId = v;
	}


	/**服务器名*/
	public get serverName() {
		return this._serverName;
	}


	/**服务器名*/
	public set serverName(v: string) {
		this._serverName = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_PlayerServerSnapshot): any {
		switch (k) {
			case E_PlayerServerSnapshot.CREATE_TIME: return this._createTime;
			case E_PlayerServerSnapshot.SERVER_ID: return this._serverId;
			case E_PlayerServerSnapshot.SERVER_NAME: return this._serverName;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_PlayerServerSnapshot, v: any): void {
		switch (k) {
			case E_PlayerServerSnapshot.CREATE_TIME: this._createTime = v; break;
			case E_PlayerServerSnapshot.SERVER_ID: this._serverId = v; break;
			case E_PlayerServerSnapshot.SERVER_NAME: this._serverName = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//创建时间
		this._createTime = 0;
		//服务器id
		this._serverId = 0;
		//服务器名
		this._serverName = '';
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._createTime) {
			json.createTime = this._createTime;
		}
		if (this._serverId) {
			json.serverId = this._serverId;
		}
		if (this._serverName) {
			json.serverName = this._serverName;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.createTime) {
			this._createTime = json.createTime;
		}
		if (json.serverId) {
			this._serverId = json.serverId;
		}
		if (json.serverName) {
			this._serverName = json.serverName;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeLongNumber(this._createTime);
		byte.writeUint16(this._serverId);
		byte.writeString(this._serverName);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._createTime = byte.readLongNumber();
		this._serverId = byte.readUint16();
		this._serverName = byte.readString();
		return this;
	}


	/**克隆*/
	public clone(): PlayerServerSnapshot {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = PlayerServerSnapshot.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = PlayerServerSnapshot.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//创建时间
			if (idx === E_PlayerServerSnapshot.CREATE_TIME) {
				const oldv = this._createTime;
				this._createTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._createTime, oldv);
				i += 2;
				continue;
			}
			//服务器id
			if (idx === E_PlayerServerSnapshot.SERVER_ID) {
				const oldv = this._serverId;
				this._serverId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._serverId, oldv);
				i += 2;
				continue;
			}
			//服务器名
			if (idx === E_PlayerServerSnapshot.SERVER_NAME) {
				const oldv = this._serverName;
				this._serverName = us[i + 1];
				IAsync.dispatch(eventID, idx, this._serverName, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in PlayerServerSnapshot with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: PlayerServerSnapshot): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._createTime !== other._createTime) {
			updates.push(E_PlayerServerSnapshot.CREATE_TIME, this._createTime);
		}
		if (this._serverId !== other._serverId) {
			updates.push(E_PlayerServerSnapshot.SERVER_ID, this._serverId);
		}
		if (this._serverName !== other._serverName) {
			updates.push(E_PlayerServerSnapshot.SERVER_NAME, this._serverName);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(PlayerServerSnapshot.TOTAL_EVENT_ID, this.getV(0));
	}

}
